package com.naiyumie.ncalculator;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.naiyumie.nlib.NWidgetTool;
import com.naiyumie.nlib.Nlog;
import com.naiyumie.stastics.StasticsBasic;

public class Stastics01Activity extends KingWangJjangActivity implements OnClickListener {
	
	String msg = null;
	EditText etd1 = null;
	TextView tvd1 = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_stastics01);
		Intent fromIntent = getIntent();
		msg = fromIntent.getStringExtra("MSG");
		this.setMsg(msg);
		this.callMethod();
		tvTitle.setText(R.string.category04);
		etd1 = (EditText) findViewById(R.id.etd1);
		etd1.addTextChangedListener(stasticsWatcher);
		etd1.setFilters(new InputFilter[] { nWidgetTool.regEditTextFilter("^[,0-9]+$") });
		tvd1 = (TextView) findViewById(R.id.tvHelperText);
	}
	
	private final TextWatcher stasticsWatcher = new TextWatcher() {
		
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}
		
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}
		
		public void afterTextChanged(Editable s) {
			String inputValue = etd1.getText().toString();
			if (!Pattern.matches("^[,0-9]+$", inputValue)) {
				nlog.puts("invalid inputValue");
				return;
			}
			List<BigDecimal> bdList = new ArrayList<BigDecimal>();
			try {
				String regExp = ",";
				String[] inputValueArray = inputValue.split(regExp);
				for (int i = 0; i < inputValueArray.length; i++) {
					bdList.add(new BigDecimal(inputValueArray[i]));
				}
			} catch (Exception e) {
				nlog.puts("error");
			} finally {
				for (int i = 0; i < bdList.size(); i++) {
					nlog.puts("one index " + i + " : value " + bdList.get(i));
				}
				BigDecimal[] bdListResult = bdList.toArray(new BigDecimal[bdList.size()]);
				StasticsBasic sb = new StasticsBasic();
				String result = "[총합과 평균치]\n";
				result += String.format("\t총합 : %s\n", sb.getSum(bdListResult)).toString();
				result += String.format("\t평균 : %s\n", sb.getAvg(bdListResult).toString());
				result += "\n";
				result += "1. 표본의 표준 편차\n";
				result += String.format("\t분산 : %s\n", sb.getVar(bdListResult, new BigDecimal("1")).toString());
				result += String.format("\t표준편차 : %s\n", sb.getStdv(bdListResult, new BigDecimal("1")).toString());
				result += String.format("\t변동계수 : %s\n", sb.coeOfVar(bdListResult, new BigDecimal("1")).toString());
				result += "\n";
				result += "2. 모집단 전체의 표준 편차\n";
				result += String.format("\t분산 : %s\n", sb.getVar(bdListResult, new BigDecimal("0")).toString());
				result += String.format("\t표준편차 : %s\n", sb.getStdv(bdListResult, new BigDecimal("0")).toString());
				result += String.format("\t변동계수 : %s\n", sb.coeOfVar(bdListResult, new BigDecimal("0")).toString());
				tvd1.setText(result);
			}
		}
	};
}
